package com.cse333.jester;

import java.io.File;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {//tabActivity is deprecated, we should use the apis for tab swiping with fragments
											   // there are api demos in android sample projects under apps, fragments and fragments tabs
											   //there is also an option to start using tabs with swiping when you create an android activity
	
	private MediaPlayer mMediaPlayer;
	
	public final static String EXTRA_MESSAGE = "com.cse333.jester.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        
        
        createTabs();
        
        
    }

    private void createTabs() 
    {
    	TabHost tabHost = getTabHost();
    	
    	
		//tab for recent
		TabSpec recentSpec = tabHost.newTabSpec(getString(R.string.title_activity_recent));
		recentSpec.setIndicator(getString(R.string.title_activity_recent));
		Intent recentIntent = new Intent(this, Recent.class);
		recentSpec.setContent(recentIntent);
		
		//tab for artists
		TabSpec artistsSpec = tabHost.newTabSpec(getString(R.string.title_activity_artists));
		artistsSpec.setIndicator(getString(R.string.title_activity_artists));
		Intent artistsIntent = new Intent(this, Artists.class);
		artistsSpec.setContent(artistsIntent);
		//tab for albums
		TabSpec albumsSpec = tabHost.newTabSpec(getString(R.string.title_activity_albums));
		albumsSpec.setIndicator(getString(R.string.title_activity_albums));
		Intent albumsIntent = new Intent(this, Albums.class);
		albumsSpec.setContent(albumsIntent);
        //tab for songs
		TabSpec songsSpec = tabHost.newTabSpec(getString(R.string.title_activity_songs));
		songsSpec.setIndicator(getString(R.string.title_activity_songs));
		Intent songsIntent = new Intent(this, Songs.class);
		songsSpec.setContent(songsIntent);
		//tab for playlists
		TabSpec playlistsSpec = tabHost.newTabSpec(getString(R.string.title_activity_playlists));
		playlistsSpec.setIndicator(getString(R.string.title_activity_playlists));
		Intent playlistsIntent = new Intent(this, Playlists.class);
		playlistsSpec.setContent(playlistsIntent);
		//tab for genres
		TabSpec genresSpec = tabHost.newTabSpec(getString(R.string.title_activity_genres));
		genresSpec.setIndicator(getString(R.string.title_activity_genres));
		Intent genresIntent = new Intent(this, Genres.class);
		genresSpec.setContent(genresIntent);
 
        // Adding all Tabs to TabHost
        tabHost.addTab(recentSpec);
        tabHost.addTab(artistsSpec);
        tabHost.addTab(albumsSpec);
        tabHost.addTab(songsSpec);
        tabHost.addTab(playlistsSpec);
        tabHost.addTab(genresSpec);
        
        
        
        
        
        try {
           
                    /**
                     * TODO: Set the path variable to a local audio file path.
                     */
                	
                	
                	String[] files = new File("/mnt/sdcard/Music").list();
                	for(int i=0; i<files.length;i++){
                		System.out.println(files[i]);
                	}
                    
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    
                    String dataSource = Environment.getExternalStorageDirectory().getPath().replace("sdcard0", "sdcard1") + "/Music/Reverie.mp3";
                   
                    mMediaPlayer.setDataSource(dataSource);
                    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                    mmr.setDataSource(dataSource);
                    
                    System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
                    System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
                    System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
//                    Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length);
//                    ImageView image=new ImageView(this);
//                    image.setImageBitmap(bmp);
                    
                    
                    mMediaPlayer.prepare();
                    
                    
                    mMediaPlayer.start();
             

        } catch (Exception e) {
            
        }
        
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        
        return true;
    }
    
    

    public void onPrepared(MediaPlayer player) {
        player.start();
    }
}
